package vcp.controllers;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.event.AncestorEvent;
import javax.swing.event.AncestorListener;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;

import db.parking.beans.rates;
import vcp.common.IController;
import vcp.models.RatesApprovalModel;
import vcp.views.RatesApprovalView;
import vcp.views.RatesManagementView;


/**
 * Rates Approval Controller
 *
 */
public class RatesApprovalController implements
ActionListener,TableModelListener,AncestorListener, IController{
	RatesApprovalModel model;
	RatesApprovalView view;
	
	RatesManagementView rmview;
	
	public RatesApprovalController(RatesManagementView ratesManagementView) {
		rmview = ratesManagementView;
	}

	@Override
	public void addModel(Object model) {
		this.model = (RatesApprovalModel)model;
	}

	@Override
	public void addView(Object view) {
		this.view = (RatesApprovalView)view;
	}

	@Override
	public void tableChanged(TableModelEvent e) {
		if(e.getType()==TableModelEvent.UPDATE){  //editing
			/* take new value go to database compare if changed do 
			 * update
			 */			
			DefaultTableModel tbm = (DefaultTableModel)e.getSource();			
			rates bean = new rates();
			bean.setLotNo(Integer.parseInt(tbm.getValueAt(e.getFirstRow(), 0).toString()));
			bean.setRateType(Integer.parseInt(tbm.getValueAt(e.getFirstRow(), 1).toString()));
			bean.setRateName(tbm.getValueAt(e.getFirstRow(), 2).toString());
			bean.setAmount(Integer.parseInt(tbm.getValueAt(e.getFirstRow(), 3).toString()));
			bean.setTmpAmount(Integer.parseInt(tbm.getValueAt(e.getFirstRow(), 4).toString()));
			boolean status = Boolean.parseBoolean(tbm.getValueAt(e.getFirstRow(), 5).toString());
			if(status==false){
			   bean.setStatus(0);
			}else{
               bean.setStatus(1);
			}
			model.approveRate(bean);
			
			//rmview.getPnlRatesManagement().getRatesTable().getDtm().fireTableChanged(e);
			//bean.setLotNo(Integer.parseInt((tbm.getValueAt(e.getFirstRow(), 0).toString())));
			//bean.setRateType(rateType);
			//System.out.println(newValue);
		}
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub
	}

	public RatesApprovalModel getModel() {
		return model;
	}

	public RatesApprovalView getView() {
		return view;
	}

	@Override
	public void ancestorAdded(AncestorEvent event) {
		int rows = view.getPnlRatesApproval().getRatesTable().getDtm().getRowCount();
		if(rows==0)
		  model.getModifiedRates();
		else{
			view.getPnlRatesApproval().getRatesTable().getDtm().setRowCount(0);;
			model.getModifiedRates();
		}
	}

	@Override
	public void ancestorMoved(AncestorEvent event) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void ancestorRemoved(AncestorEvent event) {
		// TODO Auto-generated method stub
		
	}

}
